//
//  HMStatusViewModel.swift
//  CZWeibo31
//
//  Created by apple on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMStatusViewModel: NSObject {
    // 创建一个对象model
    var statusModel: HMStatusModel?
    
    // 微博等级图片
    var mbrankImage: UIImage?
    
    // 微博认证图片
    var verifiedImage: UIImage?
    
    // 微博来源
    var sinaSourceStr: String?
    // 转发
    var repostsCountStr: String?
    // 评论
    var commentsCountStr: String?
    // 赞
    var attitudesCountStr: String?
    // 微博时间(计算型属性)
    var sinaTimeStr: String?{
        return NSDate.getSinaDate(statusModel?.created_at ?? "").sinaDateDescription
    }
    
    // 原创微博的微博正文富文本?
    var orginalAttr: NSMutableAttributedString?
    // 转发微博的微博正文富文本
    var retweetAttr: NSMutableAttributedString?

    
    init(statusModel: HMStatusModel) {
        super.init()
        self.statusModel = statusModel
        mbrankImage = dealMbrankImage(statusModel.user?.mbrank ?? 0)
        verifiedImage = dealVerifiedImage(statusModel.user?.verified ?? -1)
        sinaSourceStr = dealSinaSourceStr(statusModel.source ?? "")
        repostsCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title: "转发")
        commentsCountStr = getSinaCountStr(statusModel.comments_count ?? 0, title: "评论")
        attitudesCountStr = getSinaCountStr(statusModel.attitudes_count ?? 0, title: "赞")
        orginalAttr = sinaDealContentAttr(statusModel.text ?? "")
        retweetAttr = sinaDealContentAttr(statusModel.retweeted_status?.text ?? "")
    }
    
}


extension HMStatusViewModel {
    func sinaDealContentAttr(text: String) -> NSMutableAttributedString{
        // 把当前的字符串转成富文本
        let allTextAttr = NSMutableAttributedString(string: text)
        // 正则匹配的规则
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
        /**
         *  正则匹配的规则
         *
         *  @param NSString 正则匹配的规则
         - 匹配的个数
         - 匹配的结果
         - 匹配的范围
         - 匹配是否停止
         *
         *  @return
         */
         // 创建一个可变的数组
        var matchArray:[HMMatchModel] = [HMMatchModel]()
        (text as NSString).enumerateStringsMatchedByRegex(regex) { (count, result, range, _) -> Void in
            // 实例化model
            let model = HMMatchModel(string: (result.memory as! String), range: range.memory)
            // 添加模型到数组中
            matchArray.append(model)
        }
        
        // 倒着遍历数组
        for matchModel in matchArray.reverse() {
            // 判断他是否为nil
            guard let emoticonModel = HMEmoticonTools.sharedTools.searchEmoticonInDefaultEmotionsOrLxhEmoticons(matchModel.string) else{
                continue
            }
            // 创建一个不可变的富文本
            let attr = NSAttributedString.emoticonAttributedString(emoticonModel, textFont: 15, y: -4)
            // 替换
            allTextAttr.replaceCharactersInRange(matchModel.range, withAttributedString: attr)
        }
        
        // 返回一个替换完成后的富文本
        return allTextAttr
    }
}

// 处理底部视图 转发 评论 赞
extension HMStatusViewModel {
    /*
    - 底部视图的转发 评论 赞  业务逻辑
    - 如果 count <= 0  直接显示 转发 评论 赞
    - 如果 count > 0 && count < 10000  是多少显示多少  示例  8888  8888
    - 如果 count >= 10000   示例 12000   显示  1.2万
    - 如果 count%10000 == 0   示例 20000  显示  2万
    */
    // 处理底部视图 转发 评论 赞
    func getSinaCountStr(count: Int, title: String) -> String{
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else {
            let cFloat = CGFloat(count/1000)/10
            var cFloatStr = "\(cFloat)万"
            // 是否包含".0"
            if cFloatStr.containsString(".0") {
                
                // 字符串替换
                cFloatStr = cFloatStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            
            return cFloatStr
        }
    }
}

// 处理微博来源
extension HMStatusViewModel {
    // 处理微博来源
    func dealSinaSourceStr(source: String) -> String{
        // 判断字符串是否包含"> 特殊字符
        if source.containsString("\">") {
            // 获取range
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            // 字符串截取
            let result = source.substringWithRange(startRange.endIndex..<endRange.startIndex)
            return "来自 \(result)"
        }
        // 如果为空字符串
        return "来自 那遥远的地方"
    }
}

// 处理cell中数据的逻辑
extension HMStatusViewModel{
    // 处理微博等级image/// 会员等级 1-6
    func dealMbrankImage(mbrank: Int) -> UIImage?{
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return UIImage(named: "common_icon_membership")
    }
    
    // 处理微博用户认证image/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func dealVerifiedImage(verified: Int) -> UIImage?{
        switch verified{
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
}
